package mya.mes.mdc.logic.init;


import mya.mes.mdc.logic.alarm.build.AlarmCurrentSaver;
import mya.mes.mdc.logic.alarm.build.AlarmNoGenerator;
import mya.mes.mdc.logic.dispatch.core.AlarmDispatchManager;
import mya.mes.mdc.logic.dispatch.executor.AlarmExecutor;
import mya.mes.mdc.logic.dispatch.executor.DispatchExecutor;
import mya.mes.mdc.logic.mqtt.MdcMqttDefaultProcessor;
import mya.mes.mdc.logic.tag.PlcTagCache;
import mya.mes.mdc.logic.tag.TagService;
import mya.mes.mdc.logic.tag.impl.TagServiceImpl;
import mya.mes.mdc.mqtt.MqttClientManager;
import mya.mes.mdc.share.api.MdcInitService;
import mya.mes.mdc.share.vo.MdcOptions;

public class MdcInitServiceImpl implements MdcInitService {
    @Override
    public void init(MdcOptions mdcOptions) throws Exception {
        if (mdcOptions.MQTT_RECEIVE_DEFAULT_PROCESSOR) {
            mdcOptions.MQTT_RECEIVE_PROCESSOR = new MdcMqttDefaultProcessor();
        } else if (mdcOptions.MQTT_RECEIVE_PROCESSOR == null) {
            throw new Exception("没有定义处理接收MQTT消息的接口的实现");
        }
        //初始化报警tag
        TagService tagService = new TagServiceImpl();
        tagService.loadTagHolder();

        //初始化当前存在的报警
        AlarmCurrentSaver currentSaver = new AlarmCurrentSaver();
        currentSaver.load();
        AlarmNoGenerator.getInstance().load();

        PlcTagCache.getInstance().updateCache();
        MqttClientManager.getInstance().start();
        if (mdcOptions.ALARM_SENT_TO_WEIXIN) {
//            AlarmDispatchManager.getInstance().start();
            DispatchExecutor.getInstance().start();
            AlarmExecutor.getInstance().start();
        }
    }
}
